<template>
  <div>
    <el-card style="margin-bottom: 10px">
      <CategorySelect
        @getCategoryId="getCategoryId"
        :isShow="scene != 0"
      ></CategorySelect>
    </el-card>

    <!-- 将来是三个模块进行切换的 -->
    <el-card>
      <!-- 展示spu列表的结构 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-bottom: 10px"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>

          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>

          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>

          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 这里的button以后用hintbutton换 -->
              <hint-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></hint-button>
              <hint-button
                type="warning"
                icon="el-icon-edit"
                title="修改spu"
                @click="updateSpu(row)"
                size="mini"
              ></hint-button>
              <hint-button
                type="info"
                icon="el-icon-info"
                title="查看当前spu全部sku列表"
                @click="handler(row)"
                size="mini"
              ></hint-button>

              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="deleteSpu(row)"
              >
                <hint-button
                  type="danger"
                  icon="el-icon-delete"
                  title="删除spu"
                  size="mini"
                  slot="reference"
                ></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper, ->, sizes,total"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScenes="changeScenes"
      ></SkuForm>
    </el-card>
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuList" border style="width: 100%" v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="250">
        </el-table-column>

        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>

        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>

        <el-table-column label="默认图片" width="width">
          <template slot-scope="{ row, $index }">
            <img :src="row.skuDefaultImg" style="heigth: 100px; width: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  data() {
    return {
      category1Id: "", // 分别是分类的id
      category2Id: "", // 分别是分类的id
      category3Id: "", // 分别是分类的id
      // show: true, // 控制三级分类的可操作性
      page: 1, // 分页器当前第几页
      limit: 3, // 每一页展示多少条数据
      records: [], //SPU列表的数据
      total: 0, //分页器一共需要展示多少数据
      scene: 0, //0代表展示spu列表的数据  1代表添加或修改spu   2代表添加sku
      dialogTableVisible: false, // 控制对话款的显示隐藏
      spu: {},
      skuList: [], // 存储sku列表的数据
      loading: true, //
    };
  },
  components: { SpuForm, SkuForm },
  methods: {
    // 三级联动自定义事件,可以把子组件的相应的id传递给父组件
    getCategoryId({ CategoryId, level }) {
      //categoryId:获取到一、二、三级分类的id  level：为了区分是几级id
      if (level == 1) {
        this.category1Id = CategoryId;
        // console.log(this.category1Id);
        //清除2、3级分类的id
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = CategoryId;
        // console.log(this.category2Id);
        //清除3级id
        this.category3Id = "";
      } else {
        this.category3Id = CategoryId;
        // console.log(this.category3Id);
        // 获取spu列表进行展示
        this.getSpuList();
      }
    },
    // 获取spu列表数据的方法
    async getSpuList() {
      //携带三个参数:page 第几页  limit 每一页需要展示多少条数据  三级分类id
      const { page, limit, category3Id } = this;
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    // 点击分页器按钮的回调
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuList();
    },
    //当分页器的某一个展示数据条数发生变化的回调
    handleSizeChange(limit) {
      //修改参数
      this.limit = limit;
      //再发请求
      this.getSpuList();
    },
    // 添加spu按钮的回调
    addSpu() {
      this.scene = 1;
      // 通知子组件发请求:两个
      this.$refs.spu.addSpuData(this.category3Id);
    },
    // 修改spu按钮的回调
    updateSpu(row) {
      this.scene = 1;
      // 获取子组件spu,在父组件中可以通过$refs和$children获取子组件数据和方法
      this.$refs.spu.initSpuData(row);
    },
    // 自定义事件的回调,spuform
    changeScene({ scene, flag }) {
      // 切换场景
      this.scene = scene;
      // console.log(scene, flag);
      // 子组件发请求之后通知父亲组件切换场景,父组件重新发请求获取最新列表
      // flag按钮用来区分点击保存的时候是添加还是修改
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList(1);
      }
    },
    // 删除spu按钮的回调
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        await this.getSpuList(
          //代表SPU个数大于1删除的时候停留在当前页，如果SPU个数小于1 回到上一页
          this.records.length > 1 ? this.page : this.page - 1
        );
      }
    },
    // 添加sku按钮的回调
    addSku(row) {
      // 切换场景为2
      this.scene = 2;
      // 父组件调用子组件,让子组件发请求(三个)
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    // 自定义事件的回调,skuform
    changeScenes(scene) {
      this.scene = scene;
      // 清除数据,防止回显
      // Object.assign(this._data, this.$options.data());
    },
    // 查看sku的按钮的回调
    async handler(spu) {
      // 点击按钮显示对话框
      this.dialogTableVisible = true;
      // 保存spu的信息
      this.spu = spu;
      // 获取sku列表数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        // loding隐藏
        this.loading = false;
      }
    },
    // 关闭对话框的回调
    close(done) {
      // 修改loading属性为真
      this.loading = true;
      // 清除skuList的数据
      this.skuList = [];
      // 关闭对话框
      done();
    },
  },
};
</script>

<style>
</style>